<template>
  <Transition name="dialog">
    <div v-if="visible" class="confirm-dialog-overlay" @click="handleOverlayClick">
      <div class="confirm-dialog" @click.stop>
        <div class="dialog-header">
          <div class="dialog-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2" fill="none"/>
              <path d="M12 8v4M12 16h.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
            </svg>
          </div>
          <h3 class="dialog-title">{{ title }}</h3>
        </div>
        <div class="dialog-body">
          <p class="dialog-message">{{ message }}</p>
        </div>
        <div class="dialog-footer">
          <button class="dialog-btn cancel-btn" @click="handleCancel">
            {{ cancelText }}
          </button>
          <button class="dialog-btn confirm-btn" @click="handleConfirm">
            {{ confirmText }}
          </button>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '确认'
  },
  message: {
    type: String,
    required: true
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  closeOnOverlay: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['confirm', 'cancel', 'update:visible'])

const handleConfirm = () => {
  emit('confirm')
  emit('update:visible', false)
}

const handleCancel = () => {
  emit('cancel')
  emit('update:visible', false)
}

const handleOverlayClick = () => {
  if (props.closeOnOverlay) {
    handleCancel()
  }
}

watch(() => props.visible, (newVal) => {
  if (newVal) {
    document.body.style.overflow = 'hidden'
  } else {
    document.body.style.overflow = ''
  }
})
</script>

<style scoped>
.confirm-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(4px);
}

.confirm-dialog {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.05);
  min-width: 420px;
  max-width: 480px;
  overflow: hidden;
  position: relative;
}

/* 对话框动画 */
.dialog-enter-active {
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.dialog-leave-active {
  transition: all 0.2s ease-in;
}

.dialog-enter-from {
  opacity: 0;
  transform: scale(0.9) translateY(-10px);
}

.dialog-leave-to {
  opacity: 0;
  transform: scale(0.95);
}

.dialog-header {
  padding: 24px 24px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: linear-gradient(to bottom, #fafbfc 0%, #ffffff 100%);
  border-bottom: 1px solid #f0f0f0;
}

.dialog-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #0ea5e9;
}

.dialog-icon svg {
  width: 24px;
  height: 24px;
}

.dialog-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  color: #1e293b;
  letter-spacing: -0.01em;
}

.dialog-body {
  padding: 24px;
  background: white;
}

.dialog-message {
  margin: 0;
  font-size: 0.95rem;
  color: #64748b;
  line-height: 1.7;
  letter-spacing: 0.01em;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 20px 24px;
  background: #fafbfc;
  border-top: 1px solid #f0f0f0;
}

.dialog-btn {
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  border: none;
  min-width: 90px;
  position: relative;
  overflow: hidden;
}

.dialog-btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  transform: translate(-50%, -50%);
  transition: width 0.4s, height 0.4s;
}

.dialog-btn:hover::before {
  width: 200px;
  height: 200px;
}

.cancel-btn {
  background: white;
  color: #64748b;
  border: 1.5px solid #e2e8f0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.cancel-btn:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
  color: #475569;
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.cancel-btn:active {
  transform: translateY(0);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.confirm-btn {
  background: linear-gradient(135deg, #93c5fd 0%, #60a5fa 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(96, 165, 250, 0.25);
  border: 1px solid rgba(96, 165, 250, 0.2);
}

.confirm-btn:hover {
  background: linear-gradient(135deg, #7dd3fc 0%, #38bdf8 100%);
  box-shadow: 0 6px 20px rgba(96, 165, 250, 0.35);
  transform: translateY(-2px);
  border-color: rgba(96, 165, 250, 0.3);
}

.confirm-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(96, 165, 250, 0.25);
}

.confirm-btn:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.2), 0 6px 20px rgba(96, 165, 250, 0.35);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .confirm-dialog {
    min-width: calc(100% - 40px);
    margin: 20px;
    border-radius: 14px;
  }

  .dialog-header {
    padding: 20px 20px 16px;
  }

  .dialog-icon {
    width: 36px;
    height: 36px;
  }

  .dialog-icon svg {
    width: 20px;
    height: 20px;
  }

  .dialog-title {
    font-size: 1.05rem;
  }

  .dialog-body {
    padding: 20px;
  }

  .dialog-message {
    font-size: 0.9rem;
  }

  .dialog-footer {
    padding: 16px 20px;
    flex-direction: column-reverse;
    gap: 10px;
  }

  .dialog-btn {
    width: 100%;
    padding: 12px 20px;
  }
}
</style>

